package com.seveneight.baijiesteward.ui.main.model;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.base.MessageEvent;
import com.seveneight.baijiesteward.bean.BaseEntity;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.http.BaseModel;
import com.seveneight.baijiesteward.http.BaseObserver;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.http.RetrofitFactory;
import com.seveneight.baijiesteward.ui.main.contract.LoginContract;
import com.seveneight.baijiesteward.utils.BusUitls;
import com.seveneight.baijiesteward.utils.PreferencesUtil;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by user1 on 2018/2/23.
 */

public class LoginModel extends BaseModel implements LoginContract.LoginModel {


    @Override
    public void login(Context context,RequestBody body, MyCallBack<UserBean> callBack) {
        RetrofitFactory.getInstence().API()
                .login(body)
                .compose(this.<BaseEntity<UserBean>>setThread())
                .subscribe(new BaseObserver<UserBean>(context,"正在登录...") {
                    @Override
                    protected void onSuccees(BaseEntity<UserBean> t) throws Exception {
                        Log.d("xxxx", t.toString());
//                        PreferencesUtil.put(context,PreferencesUtil.userId, JSON.toJSONString(t.getData()));
//                        MessageEvent event=new MessageEvent(110);
//                        event.setMessage(BusUitls.LOGINSUCCESS);
//                        EventBus.getDefault().post(event);
                        callBack.onSuccess(t.getData());
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }
                });
    }

    @Override
    public void getUserInfo(String message,Context context,RequestBody body, MyCallBack<UserBean> callBack) {
        RetrofitFactory.getInstence().API()
                .getUserInfo(body)
                .compose(this.<BaseEntity<UserBean>>setThread())
                .subscribe(new BaseObserver<UserBean>(context,message) {

                    @Override
                    protected void onSuccees(BaseEntity<UserBean> t) throws Exception {
                        Log.d("userInfo:", t.toString());
                        UserBean bean=JSON.parseObject(JSON.toJSONString(t.getData()),UserBean.class);
                        PreferencesUtil.put(context,PreferencesUtil.userId, bean.getUserID());

                        PreferencesUtil.put(context,PreferencesUtil.userinfo, JSON.toJSONString(t.getData()));
                        MessageEvent event=new MessageEvent(110);
                        event.setMessage(BusUitls.LOGINSUCCESS);
                        EventBus.getDefault().post(event);
                        callBack.onSuccess(t.getData());
                    }

                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }
                });
    }
}
